package com.javaWork.service.impl;

import com.javaWork.entity.Pet;
import com.javaWork.entity.PetsOfUser;
import com.javaWork.mapper.PetMapper;
import com.javaWork.service.PetService;
import com.javaWork.service.PetsOfUserService;
import com.javaWork.tools.PublicFieldAndFunction;
import com.javaWork.tools.StatusCode;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CheHan
 * @date 2022/4/8 23:49
 */
@Service
public class PetServiceImpl implements PetService {

    private final PetMapper petMapper;
    private final PetsOfUserService petsOfUserService;

    public PetServiceImpl(PetMapper petMapper, PetsOfUserService petsOfUserService) {
        this.petMapper = petMapper;
        this.petsOfUserService = petsOfUserService;
    }

    @Override
    public int addPet(Pet pet) {
        return petMapper.addPets(pet);
    }

    @Override
    public List<Pet> queryPet(int number, String kind, String variety) {
        return petMapper.queryPet(number, kind, variety);
    }

    @Override
    public List<String> queryVarietiesByKind(String kind) {
        List<String> varieties;
        List<String> returnList = new ArrayList<>();
        if (PublicFieldAndFunction.PET_SELECT_FIELD_ALL.equals(kind)) {
            varieties = petMapper.queryVarietiesByKind(null);
        } else {
            varieties = petMapper.queryVarietiesByKind(kind);
        }

        for (String variety : varieties) {
            if (!returnList.contains(variety)) {
                returnList.add(variety);
            }
        }
        return returnList;
    }

    @Override
    public int updatePet(@NotNull Map<Object, Object> petMap) {
        if ((!"".equals(petMap.get(PublicFieldAndFunction.PET_FIELD_KIND)))
                || (!"".equals(petMap.get(PublicFieldAndFunction.PET_FIELD_VARIETY)))
                || (petMap.get(PublicFieldAndFunction.PET_FIELD_PRICE) != null)
                || (petMap.get(PublicFieldAndFunction.PET_FIELD_COUNT) != null)) {
            int statusCode = petMapper.updatePets(petMap);
            System.out.println(statusCode);
            if (statusCode >= 1) {
                return StatusCode.STATUS_OK;
            } else {
                return StatusCode.INTERNAL_ERROR;
            }
        } else {
            return StatusCode.NO_CHANGE;
        }
    }

    @Override
    public int deletePetByNumber(int number) {
        int statusCode = petMapper.deletePetsByNumber(number);

        if (statusCode >= 1) {
            return StatusCode.STATUS_OK;
        } else {
            return StatusCode.INTERNAL_ERROR;
        }
    }

    @Override
    public int buyPets(String userName, int number, int count) {

        List<Pet> pets = petMapper.queryPet(number, null, null);
        if (pets.size() < 1) {
            return StatusCode.NOT_FOUND;
        }

        Pet pet = pets.get(0);
        if (pet.getCount() < count) {
            return StatusCode.NOT_ENOUGH;
        }

        HashMap<Object, Object> map = new HashMap<>(PublicFieldAndFunction.PET_HASHMAP_INITIAL_CAPACITY);
        map.put(PublicFieldAndFunction.PET_FIELD_NUMBER, number);
        map.put(PublicFieldAndFunction.PET_FIELD_COUNT, pet.getCount() - count);

        int updStatus = petMapper.updatePets(map);
        if (updStatus < 1) {
            return StatusCode.INTERNAL_ERROR;
        }

        int addPetsForUserStatus = petsOfUserService.addPetsForUser(userName,
                new PetsOfUser(pet.getKind(), pet.getVariety(), count, pet.getPrice() * count));
        if (addPetsForUserStatus < 1) {
            return StatusCode.INTERNAL_ERROR;
        }

        return StatusCode.STATUS_OK;
    }
}
